*** this do-file produces tables 5-9 and figures 6,7

cd "path_to_replication_folder"
sysdir set PLUS "path_to_nondefault_stata_packages"


u marriages, clear
drop if year<2005
merge 1:m lnr year using dpowners_shares 
keep if _merge==3
drop _merge id lnr_for
rename lnr lnr_Ii
rename s s_i
rename lnr_a lnr_i
rename lnr_p lnr
sort lnr year
joinby lnr year using dpowners_shares
rename s s_j
rename lnr_a lnr_j
rename lnr lnr_Ij
drop id lnr_for

g s05_i=s_i if s_i>.05
g s1_i=s_i if s_i>.1

g s05_j=s_j if s_j>.05
g s1_j=s_j if s_j>.1

drop s_i s_j


* unique marriage id
egen mxl = rowmax(lnr_I*)
egen mnl = rowmin(lnr_I*)
egen mid=group(mxl mnl)
drop mxl mnl

rename lnr_i lnr_as_orgnr
merge m:1 lnr_as_orgnr year using balance_sheets, keepusing(bof) keep(match)
drop _merge
rename lnr_as lnr_i
rename bof sec_i

rename lnr_j lnr_as_orgnr
merge m:1 lnr_as_orgnr year using balance_sheets, keepusing(bof) keep(match)
drop _merge

rename lnr_as lnr_j
rename bof sec_j

compress
g sec3_i=floor(sec_i*10) / 10
g sec3_j=floor(sec_j*10) / 10
drop if lnr_i == lnr_j

foreach x in 1 05 {
preserve
keep if s`x'_j!=. & s`x'_i!=.  
* define treatment
g t=1 if sec_i == sec_j   
drop s* 
rename year myear
keep t myear lnr_j mid  // keep j-side of the symmetric dataset
duplicates drop mid lnr_j t, force  // control firms get paired with several other control firms
duplicates tag mid lnr_j, g(tag)
drop if tag>0 & t==.  // treated firms also get paired with untreated ones
drop tag
replace t=0 if t==.
egen T = mean(t), by(lnr_j)
tab T
drop if T>0 & T<1  // do not use treated firms as controls for other treated ones
drop T
save treated_control`x', replace
keep lnr 
duplicates drop 
save tcfirms`x', replace
restore
}


foreach x in 1 05 {
u balance_sheetsw, clear
merge m:1 lnr_j using tcfirms`x' 
keep if _m==3
drop _m
joinby lnr_j using treated_control`x' //* firms can be control firms in multiple marriages (there are multiple owners in a firm that can marry) 
g time = year-myear
save treated_control_rs`x', replace
}



** MAIN: marriage events, 5% cutoff

u treated_control_rs05, clear
g T=1 if t==1 & year==myear
replace T=0 if T==. 

egen pid=group(lnr mid)
tsset pid time
forvalues i = 1(1)3 {
	g Tl`i' = l`i'.T  
	g Tf`i' = f`i'.T
}

keep if time>-4 & time<3
g post =1 if time>-1 
replace post = 0 if post==.
g tpost = 1 if post==1 & t==1
replace tpo=0 if tpo==.
egen bsec=group(sec1)
gen sec2=floor(bof)

global coeflab "1 "-3" 2 "-2" 3 "-1" 4 "0" 5 "1" 6 "2" "

*baseline period
replace Tf1=0 if Tf1!=.

egen Nmid=count(t), by(mid)
egen tt=total(t), by(mid) 

eststo within: reghdfe sr2 Tf3 Tf2 Tf1 T Tl1 Tl2 if tt>0 & tt!=Nmid, ab(lnr_j mid#time sec2#year) cluster(bof)   
coefplot, vert xlabel($coeflab) keep(Tf3 Tf2 Tf1 T Tl1 Tl2) omitted ci(90) graphregion(color(white))  yline(0, lcolor(gs4) lwidth(.5)) ciopts(color(gs6) lwidth(.5)) msize(2)  ylabel(-.2(.1).4)

graph export fig6a.pdf, replace

savesome using estsample_within if _est_within==1, replace // save for balancing test

eststo within_reg: reghdfe sr2 tpost if _est_within==1, ab(lnr_j mid#time sec2#year) cluster(bof) noconst


eststo across: reghdfe sr2 Tf3 Tf2 Tf1 T Tl1 Tl2 if t==1 | tt==0, ab(lnr_j bof#year) cluster(bof)
coefplot, vert xlabel($coeflab) keep(Tf3 Tf2 Tf1 T Tl1 Tl2) omitted ci(90) graphregion(color(white)) yline(0, lcolor(gs4) lwidth(.5)) ciopts(color(gs6) lwidth(.5)) msize(2) ylabel(-.05(.05).15)
graph export fig6b.pdf, replace
savesome using estsample_across if _est_across==1, replace

eststo across_reg: reghdfe sr2 tpost if _est_across==1, ab(lnr_j bof#year) cluster(bof) noconst


global coeflabs "Tf3 "$ T_{t+3}$" Tf2 "$ T_{t+2}$" Tf1 "$ T_{t+1}$" T "$ T_{t}$" Tl1 "$ T_{t-1}$" Tl2 "$ T_{t-2}$" Tl3 "$ T_{t-3}$" tpost "$ T\times Post$""

esttab within within_reg across across_reg using tab7.tex, coeflabels($coeflabs)  b(3) se star( * .1 ** .05 *** .01) replace  nogaps  nonotes nomtitles nonumbers  drop(_cons) fragment nolines noobs substitute("\_","_") tex

esttab within within_reg across across_reg using tab7_base.tex, replace  nogaps  nonotes nomtitles nonumbers  drop(*) fragment nolines


****** Robustness 

g lns=ln(p9000)
g RD2=p1000/eiendeler
g lnS= ln(sales)


global coeflabs2 " tpost "$ T\times Post$""

eststo within_lns: reghdfe lns tpost if _est_within==1, ab(lnr_j mid#time sec2#year) cluster(bof) noconst
eststo within_RD2: reghdfe RD2 tpost if _est_within==1, ab(lnr_j mid#time sec2#year) cluster(bof) noconst  
eststo within_lnS: reghdfe lnS tpost if _est_within==1, ab(lnr_j mid#time sec2#year) cluster(bof) noconst

eststo across_lns: reghdfe lns tpost if _est_across==1, ab(lnr_j bof#year) cluster(bof) noconst
eststo across_RD2: reghdfe RD2 tpost if _est_across==1, ab(lnr_j bsec#year) cluster(bof) noconst
eststo across_lnS: reghdfe lnS tpost if _est_across==1, ab(lnr_j bof#year) cluster(bof) noconst


esttab within_lns within_lnS within_RD2 across_lns across_lnS  across_RD2 using tab9.tex, coeflabels($coeflabs2)  b(3) se star( * .1 ** .05 *** .01) replace  nogaps  nonotes nomtitles nonumbers  fragment nolines noobs substitute("\_","_") tex

esttab within_lns within_lnS within_RD2 across_lns across_lnS  across_RD2 using tab9_base.tex, replace  nogaps  nonotes nomtitles nonumbers  drop(*) fragment nolines






** BALANCing TEST

u estsample_within, clear
distinct mid
distinct lnr if t==1
distinct lnr if t==0
duplicates drop lnr year, force
keep if time<0 // only pre-treatment period

*** convert to USD (using 2011 Exchange rate)
foreach var of varlist p9000 p5000 p2000 p2800  {
replace `var'=`var'/5.961
}

sum sr2 if t==1
matrix m = r(mean)
foreach var of varlist p9000 p5000 p2000 p2800  {
sum `var' if t==1
matrix m = (m \ r(mean))
}
matlist m

sum sr2 if t==0
matrix n = r(mean)
foreach var of varlist p9000  p5000 p2000 p2800  {
sum `var' if t==0
matrix n = (n \ r(mean))
}
matlist n


eststo: reghdfe sr2 t
matrix a= r(table)
matrix b= (a[1,1], a[4,1])
eststo: reghdfe p9000 t
matrix a= r(table)
matrix b= (b \ a[1,1], a[4,1])
eststo: reghdfe p5000 t
matrix a= r(table)
matrix b= (b \ a[1,1], a[4,1])
eststo: reghdfe p2000 t
matrix a= r(table)
matrix b= (b \ a[1,1], a[4,1])
eststo: reghdfe p2800 t
matrix a= r(table)
matrix b= (b \ a[1,1], a[4,1])



reghdfe sr2 t, absorb(bsec#year)
matrix a= r(table)
matrix c= (a[1,1], a[4,1])
reghdfe p9000 t, absorb(bsec#year)
matrix a= r(table)
matrix c= (c \ a[1,1], a[4,1])
reghdfe p5000 t, absorb(bsec#year)
matrix a= r(table)
matrix c= (c \ a[1,1], a[4,1])
reghdfe p2000 t, absorb(bsec#year)
matrix a= r(table)
matrix c= (c \ a[1,1], a[4,1])
reghdfe p2800 t, absorb(bsec#year)
matrix a= r(table)
matrix c= (c \ a[1,1], a[4,1])

matrix d= (m, n, b , c)

matrix rownames d = "profit margin"  "operating income" "wage bill" "share capital" "dividend"
matrix colnames d = treated control "$\Delta$" "$ p(\Delta=0)$" "$\Delta$" "$ p(\Delta=0)$"

esttab matrix(d,  fmt(%9.2f)) using tab5.tex, tex replace fragment nomtit  nonum nodepv





u estsample_across, clear
distinct mid
distinct lnr if t==1
distinct lnr if t==0

duplicates drop lnr year, force
keep if time<0 // only pre-treatment period
reghdfe sr2 t, noconst

*** convert to USD (using 2011 Exchange rate)

foreach var of varlist p9000 p5000 p2000 p2800  {
replace `var'=`var'/5.961
}

sum sr2 if t==1
matrix m = r(mean)
foreach var of varlist p9000 p5000 p2000 p2800  {
sum `var' if t==1
matrix m = (m \ r(mean))
}
matlist m

sum sr2 if t==0
matrix n = r(mean)
foreach var of varlist p9000 p5000 p2000 p2800 {
sum `var' if t==0
matrix n = (n \ r(mean))
}
matlist n



eststo: reghdfe sr2 t
matrix a= r(table)
matrix b= (a[1,1], a[4,1])

eststo: reghdfe p9000 t
matrix a= r(table)
matrix b= (b \ a[1,1], a[4,1])

eststo: reghdfe p5000 t
matrix a= r(table)
matrix b= (b \ a[1,1], a[4,1])
eststo: reghdfe p2000 t
matrix a= r(table)
matrix b= (b \ a[1,1], a[4,1])
eststo: reghdfe p2800 t
matrix a= r(table)
matrix b= (b \ a[1,1], a[4,1])


reghdfe sr2 t, absorb(bof#year)
matrix a= r(table)
matrix c= (a[1,1], a[4,1])

reghdfe p9000 t, absorb(bof#year)
matrix a= r(table)
matrix c= (c \ a[1,1], a[4,1])
reghdfe p5000 t, absorb(bof#year)
matrix a= r(table)
matrix c= (c \ a[1,1], a[4,1])
reghdfe p2000 t, absorb(bof#year)
matrix a= r(table)
matrix c= (c \ a[1,1], a[4,1])
reghdfe p2800 t, absorb(bof#year)
matrix a= r(table)
matrix c= (c \ a[1,1], a[4,1])

matrix d= (m, n, b , c)

matrix rownames d = "profit margin"  "operating income" "wage bill" "share capital" "dividend"
matrix colnames d = treated control "$\Delta$" "$ p(\Delta=0)$" "$\Delta$" "$ p(\Delta=0)$"

esttab matrix(d,  fmt(%9.2f)) using tab6.tex, tex replace fragment nomtit nonum nodepv






** Robustness 10% cutoff

u treated_control_rs1, clear
capture drop T
g T=1 if t==1 & year==myear
replace T=0 if T==. 

egen pid=group(lnr mid)
tsset pid time
forvalues i = 1(1)3 {
	g Tl`i' = l`i'.T  
	g Tf`i' = f`i'.T
}

keep if time>-4 & time<3
g post =1 if time>-1 
replace post = 0 if post==.
g tpost = 1 if post==1 & t==1
replace tpo=0 if tpo==.

gen sec2=floor(bof)

global coeflab "1 "-3" 2 "-2" 3 "-1" 4 "0" 5 "1" 6 "2" "

replace Tf1=0 if Tf1!=.

egen Nmid=count(t), by(mid)
egen tt=total(t), by(mid)  

eststo within: reghdfe sr2 Tf3 Tf2 Tf1 T Tl1 Tl2 if tt>0 & tt!=Nmid, ab(lnr_j mid#time sec2#year) cluster(bof)   
coefplot, vert xlabel($coeflab) keep(Tf3 Tf2 Tf1 T Tl1 Tl2) omitted ci(90) graphregion(color(white))  yline(0, lcolor(gs4) lwidth(.5)) ciopts(color(gs6) lwidth(.5)) msize(2)  ylabel(-.2(.1).4)
graph export fig7a.pdf, replace


eststo within_reg: reghdfe sr2 tpost if _est_within==1, ab(lnr_j mid#time sec2#year) cluster(bof) noconst
savesome using estsample_within1 if _est_within==1, replace

eststo across: reghdfe sr2 Tf3 Tf2 Tf1 T Tl1 Tl2 if t==1 | tt==0, ab(lnr_j bof#year) cluster(bof)
coefplot, vert xlabel($coeflab) keep(Tf3 Tf2 Tf1 T Tl1 Tl2) omitted ci(90) graphregion(color(white)) yline(0, lcolor(gs4) lwidth(.5)) ciopts(color(gs6) lwidth(.5)) msize(2) ylabel(-.05(.05).15)
graph export fig7b.pdf, replace
savesome using estsample_across1 if _est_across==1, replace

eststo across_reg: reghdfe sr2 tpost if _est_across==1, ab(lnr_j bof#year) cluster(bof)

*size of treated and control group

u estsample_across1, clear
distinct mid
distinct lnr if t==1
distinct lnr if t==0

u estsample_within1, clear
distinct mid
distinct lnr if t==1
distinct lnr if t==0



erase estsample_across1.dta
erase estsample_within1.dta
erase estsample_across.dta
erase estsample_within.dta
erase treated_control05.dta
erase treated_control1.dta
erase treated_control_rs05.dta
erase treated_control_rs1.dta
erase tcfirms05.dta
erase tcfirms1.dta











**** regressions in the full sample



u balance_sheetsw, clear

rename lnr_j lnr_i
merge 1:1 lnr_i year using kraw_ii
drop if _merge==2
drop _mer

foreach x in 05 1 {
replace s`x'=0 if s`x'==.
}

merge m:1 lnr_i year using alt_iv
drop if _m==2
drop _m
foreach var of varlist  U* {
	replace `var' =0 if `var'==.
}

egen sid=group(bof)
egen fid=group(lnr_i)

capture log close
log using ivreg, replace

g crossDum1=0
replace crossDum1=1 if s1>0
g crossDum05=0
replace crossDum05=1 if s05>0


rename (s1 UM1 crossDum1) (s UM crossDum)

eststo ols1b: reghdfe sr2 s,  absorb(fid sid#year) cluster(sid)
eststo ols1a: reghdfe sr2 crossDum if _est_ols1b==1,  absorb(fid sid#year) cluster(sid)
eststo iv1b: ivreghdfe sr2 (s = UM)  if _est_ols1b==1, absorb(fid sid#year) cluster(sid) first
eststo iv1a: ivreghdfe sr2 (crossDum = UM)  if _est_ols1b==1, absorb(fid sid#year) cluster(sid) first


rename (s UM crossDum) (s1 UM1 crossDum1)
rename (s05 UM05 crossDum05) (s UM crossDum)

eststo ols05b: reghdfe sr2 s,  absorb(fid sid#year) cluster(sid)
eststo ols05a: reghdfe sr2 crossDum if _est_ols05b==1,  absorb(fid sid#year) cluster(sid)
eststo iv05b: ivreghdfe sr2 (s = UM)  if _est_ols05b==1, absorb(fid sid#year) cluster(sid) first
eststo iv05a: ivreghdfe sr2 (crossDum = UM)  if _est_ols05b==1, absorb(fid sid#year) cluster(sid) first

rename (s UM crossDum) (s05 UM05 crossDum05)


global coeflabs "s "TotalCrossOwn" crossDum "CrossDummy" UM "$\mathcal{I}_{marriage}$" UD "$\mathcal{I}_{divorce}$""


esttab ols05a ols05b iv05a iv05b ols1a ols1b iv1a iv1b

esttab ols05a ols05b iv05a iv05b ols1a ols1b iv1a iv1b using tab8.tex, coeflabels($coeflabs)  b(3) se star( * .1 ** .05 *** .01) replace  nogaps  nonotes nomtitles nonumbers  drop(_cons) fragment nolines noobs

esttab ols05a ols05b iv05a iv05b ols1a ols1b iv1a iv1b using tab8_base.tex, replace  nogaps  nonotes nomtitles nonumbers  drop(*) fragment nolines

*cleaning up
log close
erase alt_iv.dta
erase dpowners_shares.dta
erase marriages.dta
erase kraw_ii.dta
erase balance_sheetsw.dta

